"""
Minimax AI对战模式界面
"""

import pygame
from rules import GobangRules
from ai import MinimaxAI
from .base_game import BaseGameGUI
from .color_choice_dialog import ColorChoiceDialog


class MinimaxAIGameGUI(BaseGameGUI):
    """Minimax AI对战游戏界面类"""
    
    def __init__(self, board_size=15, cell_size=40, ai_depth=3):
        """初始化Minimax AI对战界面"""
        # 先选择颜色
        dialog = ColorChoiceDialog()
        player_color = dialog.run()
        
        if player_color is None:
            self.cancelled = True
            return
        
        self.cancelled = False
        self.player_color = player_color
        self.ai_color = 3 - player_color
        
        super().__init__(board_size, cell_size)
        pygame.display.set_caption("五子棋 - Minimax AI对战")
        self.mode_name = "Minimax AI对战"
        
        self.ai_player = MinimaxAI(self.ai_color, depth=ai_depth)
        self.ai_thinking = False
        
        if self.player_color == 2:
            self.ai_first_move()
    
    def ai_first_move(self):
        """AI先手时的第一步"""
        center = self.board_size // 2
        self.game_rules.make_move(center, center)
    
    def handle_player_move(self, row, col):
        """
        处理玩家落子
        :param row: 行坐标
        :param col: 列坐标
        """
        if not self.game_rules.game_over and not self.ai_thinking:
            if self.game_rules.current_player == self.player_color:
                if self.game_rules.make_move(row, col):
                    self.draw_board()
                    self.draw_pieces()
                    self.draw_status()
                    self.draw_buttons()
                    pygame.display.flip()
                    
                    if not self.game_rules.game_over:
                        self.ai_thinking = True
    
    def ai_move(self):
        """执行AI落子"""
        if self.ai_thinking and not self.game_rules.game_over:
            move = self.ai_player.get_move(self.game_rules)
            if move:
                row, col = move
                self.game_rules.make_move(row, col)
            self.ai_thinking = False
    
    def undo_move(self):
        """悔棋 - AI模式悔两步"""
        self.game_rules.undo_move()  # 撤销AI的落子
        self.game_rules.undo_move()  # 撤销玩家的落子
        self.ai_thinking = False
    
    def get_game_result_text(self):
        """获取游戏结果文本"""
        # 检查是否是禁手
        if hasattr(self.game_rules, 'forbidden_reason') and self.game_rules.forbidden_reason:
            if self.player_color == 1:
                return f"你触发{self.game_rules.forbidden_reason}，AI获胜！"
            else:
                return f"AI触发{self.game_rules.forbidden_reason}，你赢了！"
        
        if self.game_rules.winner == 0:
            return "平局！"
        elif self.game_rules.winner == self.player_color:
            return "恭喜你赢了！"
        else:
            return "AI获胜！"
    
    def get_status_text(self):
        """获取状态文本"""
        if self.game_rules.game_over:
            if self.game_rules.winner == 0:
                return "平局！"
            elif self.game_rules.winner == self.player_color:
                return "你赢了！"
            else:
                return "AI获胜！"
        else:
            if self.ai_thinking:
                return "AI深度思考中..."
            elif self.game_rules.current_player == self.player_color:
                player_name = "黑棋" if self.player_color == 1 else "白棋"
                return f"你的回合（{player_name}）"
            else:
                ai_name = "白棋" if self.ai_color == 2 else "黑棋"
                return f"AI的回合（{ai_name}）"
